<template>
<div class="coupon-client-fissure views-container">
    <div class="flex-row flex-justify-b">
        <div class="wlm-form" style="flex-grow: 1;margin-right: 20px;">
            <div class="wlm-form-header">关注收藏有礼</div>
            <div class="wlm-form-content table-content">
                <el-form ref="orther" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="180px" class="retail-form" label-position="right">
                    <el-form-item label="活动名称">
                        <el-input v-model="formFormatData.formData.voucher_title" placeholder=""></el-input>
                    </el-form-item>
                    <el-form-item label="活动时间">
                        <el-date-picker style="width:360px;" :picker-options="afterOptions" v-model="formFormatData.formData.voucher_time" :default-time="[`${`${new Date().getHours()}`.padStart(2,'0')}:${`${new Date().getMinutes()+2}`.padStart(2,'0')}:00`,'23:59:59']" value-format="timestamp" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="活动类型">
                        <el-radio-group v-model="formFormatData.formData.voucher_type">
                            <el-radio :label="5">关注公众号</el-radio>
                            <el-radio :label="6">收藏小程序</el-radio>
                        </el-radio-group>
                        <p class="form-help">每个用户仅能参加一次关注公众号和收藏小程序，已经参加过的用户即使取消关注或收藏也无法再参加</p>
                    </el-form-item>
                    <el-form-item label="优惠券">
                        <el-button type="primary" @click="couponToogle" style="display: block;margin: 12px 0;">添加优惠券</el-button>
                        <p class="form-help">可以添加最多五个优惠券</p>
                        <choose-coupon ref="chooseCoupon" :config="chooseCouponConfig" :changeEvt="filtersCouponChange"></choose-coupon>
                        <el-table class="coupon-table" v-show="formFormatData.formData.coupon!=''" :data="formFormatData.formData.coupon" style="width: 100%;margin-top:20px;">
                            <el-table-column prop="date" label="优惠券名称">
                                <template slot-scope="scope">
                                    <p>{{scope.row.name}}</p>
                                </template>
                            </el-table-column>
                            <el-table-column prop="date" label="优惠券内容">
                                <template slot-scope="scope">
                                    <couponRender>
                                        <template slot="title" v-if="scope.row.coupon_type">
                                            {{`${scope.row.rule === 0 ? `${coupon_price[`${scope.row.coupon_type}`]['textRule']}${scope.row.rule_min_money}元`:''} ${coupon_price[`${scope.row.coupon_type}`]['textPrev']}`}}
                                        </template>
                                        <template slot="price">
                                            {{scope.row.coupon_type === 40 ? `${scope.row.min} - ${scope.row.max}` : scope.row.coupon_type === 20 ? scope.row.discount : scope.row.reduce_price}}
                                        </template>
                                        <template slot="tail" v-if="scope.row.coupon_type">
                                            {{coupon_price[`${scope.row.coupon_type}`]['textLast']}}
                                        </template>
                                    </couponRender>
                                </template>
                            </el-table-column>
                            <el-table-column prop="date" label="赠券数(每一个用户可获得的优惠券个数)" min-width="100">
                                <template slot-scope="scope">
                                    <el-input-number style="margin-right:20px;" @change="oldGiftChange(scope.row,'coupon_number')" v-model="scope.row.coupon_number" controls-position="right" :min="1"></el-input-number>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" >
                                <template slot-scope="scope">
                                    <div class="operation-group">
                                        <el-button class="wlm-text" type="text" @click="toggleSelection(scope)">删除</el-button>
                                    </div>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-form>
            </div>
        </div>

        <div class="wlm-form" style="width:300px">
            <div class="wlm-form-header">关注收藏有礼预览</div>
            <div class="wlm-form-content table-content">
                <img style="width:100%" src="@/assets/couponClient/preview.png" alt="">
            </div>
        </div>
    </div>
    <div v-cloak class="wlm-fixed-btn flex-row flex-align-c flex-justify-c">
        <el-button size="small" @click="saveForm({ router: { type: 'replace', path: '/application/couponClient', code: 1 }})" type="primary">立即发券</el-button>
        <router-link tag="span" to="/application/couponClient">
            <el-button size="small">返回</el-button>
        </router-link>
    </div>
</div>
</template>

<script>
import ChooseFiles from '@/components/ChooseFiles/index'
import ChooseCoupon from '@/components/ChooseCoupon/index'
import {
  voucherDetail,
  voucherAddEdit
} from '@/api/application'
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Form')],
  name: 'CouponClientFissure',
  components: {
    ChooseFiles,
    ChooseCoupon,
    couponRender: {
      template: '<span><slot name="title"></slot><span class="text-danger"><slot name="price"></slot></span><slot name="tail"></slot></span>'
    }
  },
  computed: {
  },
  created() {
  },
  data() {
    return {
      coupon_type: {
        10: '满减券',
        20: '折扣券',
        30: '运费券',
        40: '随机券'
      },
      coupon_price: {
        10: {
          textRule: '满',
          textPrev: '减免',
          textLast: '元'
        },
        20: {
          textRule: '满',
          textPrev: '打',
          textLast: '折'
        },
        30: {
          textRule: '满',
          textPrev: '减免',
          textLast: '元'
        },
        40: {
          textRule: '满',
          textPrev: '范围内随机',
          textLast: '元'
        }
      },
      diyList: [],
      saveCustomDataList: [],
      dialogUrl: 1,
      currentRow: [],
      chooseCouponConfig: {
        type: ['chooseCoupon'],
        model: 'display',
        get_limit_type: 0,
        choose_num: 5,
        disabled: [{
          label: 'status',
          value: 1,
          reverse: true
        },
        {
          label: 'get_limit_type',
          value: 1
        }, {
          label: 'coupon_type',
          value: 40
        }],
        initList: []
      },
      formFormatData: {
        key: ['orther'],
        api: {
          editForm: {
            api: voucherDetail,
            params: {
              id: ''
            },
            redirect: 'voucher_id'
          },
          submitForm: voucherAddEdit
        },
        formData: {
          voucher_time: [],
          voucher_title: '',
          voucher_type: 5,
          coupon: []
        },
        rules: {}
      }
    }
  },
  mounted() {

  },
  methods: {
    saveForm(params) {
      this.formFormatData.formData.coupon.map(item => {
        if (this.formFormatData.formData.voucher_type === 5) {
          item.voucher_coupon_term = 4
        } else if (this.formFormatData.formData.voucher_type === 6) {
          item.voucher_coupon_term = 3
        }
      })
      this.formFormatSubmit(params)
    },
    oldGiftChange(params, key) {
      this.$nextTick(() => {
        params[key] = String(params[key]).replace(/[^\d]/g, '')
      })
    },
    filtersCouponChange(arg) {
      const { displayData } = arg
      console.log(displayData, 'display')
      displayData.map(item => {
        var hasIndex = this.formFormatData.formData.coupon.findIndex(tableitem => { return `${tableitem.coupon_id}` === `${item.coupon_id}` })
        if (hasIndex === -1 && this.formFormatData.formData.coupon.length < 5) {
          this.formFormatData.formData.coupon.push({ coupon_id: item.coupon_id, name: item.name, coupon_type: item.coupon_type, rule: item.rule, coupon_price: item.coupon_price, rule_min_money: item.rule_min_money, min: item.min, max: item.max, discount: item.discount, reduce_price: item.reduce_price })
        }
      })
    },
    couponToogle() {
      this.$refs.chooseCoupon.toggle()
    },
    toggleSelection(scope) {
      this.formFormatData.formData.coupon.splice(scope.$index, 1)
      this.currentRow.splice(scope.$index, 1)
    }
  }
}
</script>

<style lang="scss" scoped>
.coupon-client-fissure {

    /deep/ .coupon-table .el-input,
    /deep/ .coupon-table .el-input-number {
        width: 170px;
    }

    .table-raido-header {
        background: #f2f2f2;
        padding: 10px;
        font-weight: 700;
        color: #909399;
    }

    .table-raido-body {
        padding: 10px;
        line-height: 24px;
        border-bottom: 1px solid rgb(235, 238, 245);
    }

    .table-left {
        width: 15%;
    }

    .table-mid {
        width: 34%;
        margin-right: 20px;
    }

    .table-right {
        width: 49%;
    }

    /deep/ .sortable-ghost {
        opacity: .8;
        color: #fff !important;
        background: #79bbff !important;
    }

    .logo {
        height: 40px;
        width: 40px;
        border-radius: 50%;
        vertical-align: middle;
        margin-right: 6px;
    }
}
</style>
